package com.gkwave.action.servelt;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gkwave.module.bean.Login;
import com.gkwave.module.bean.User;
import com.gkwave.module.dao.UserDAO;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String username = request.getParameter("username");
    	String password = request.getParameter("password");
    	
    	PrintWriter out = response.getWriter();
    	String status = null;
    	String message = null;
    	
    	String query = "select * from USER where username='"+username+"' and password='"+password+"'";
    	UserDAO userDAO = new UserDAO();
    	User user = null;
		try {
			user = userDAO.findUser(query);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	if (user != null) {
    	    request.getSession().setAttribute("user", user); // Put user in session.
    	    status = "SUCCESS";
    	    message = "Login Successful";
//    	    response.sendRedirect("/secured/home.jsp"); // Go to some start page.
    	} else {
    		status = "ERROR";
    	    message = "Login Failed";
//    	    request.setAttribute("error", "Unknown login, try again"); // Set error msg for ${error}
//    	    request.getRequestDispatcher("/login.jsp").forward(request, response); // Go back to login page.
    	}
    	Login login = new Login(status , message);
    	out.println(login.getXMLResponds());
    	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("Am in servlet doPost!!!");
		processRequest(request, response);
	}

}
